package lp2.projeto.agenda;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;

import javax.swing.*;

import lp2.projeto.gerenciador.Evento;
import lp2.projeto.horario.Horario;
import lp2.projeto.horario.Mes;

public class CalendarioJDialog extends JDialog {

	private static final long serialVersionUID = 1L;
	private JButton[][] calendarioJButton = new JButton[6][7];
	private JButton cadastroEventoJButton;
	private JButton voltarJButton;
	private JButton modificaEventoJButton;
	private JButton removerEventoJButton;
	private JComboBox mesJComboBox;
	private JComboBox anoJComboBox;
	protected JPanel calendarioInternoJPanel;
	private JScrollPane listaEventosJScrollPane;
	private JList listaEventosJList;
	private DefaultListModel listaEventosListModel;
	int diaSelecionado;
	 public CalendarioJDialog(){
		 super();
		 setModal(true);
		 setSize(new Dimension(430,410));
		 setResizable(false);
		 JPanel calendarioJPanel = (JPanel) this.getContentPane();
		 calendarioJPanel.setLayout(new GridBagLayout());
		 calendarioJPanel.setBackground(Color.GRAY);
		 GridBagConstraints constraints = new GridBagConstraints();
		 
		 mesJComboBox = new JComboBox(Mes.values());
		 mesJComboBox.setSelectedIndex(Horario.getHoraAtual().getMes().getNumero() - 1);
		 mesJComboBox.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				 modificaCalendarioInterno(
						 (Integer) anoJComboBox.getSelectedItem(),
						 (Mes) mesJComboBox.getSelectedItem()
						 );	
			}	
		});
		 
		 Integer[] listaAnos = new Integer[111];
		 for (int i = 2011; i > 1900; i--)	listaAnos[2011-i] = i;	
		 anoJComboBox = new JComboBox(listaAnos);
		 anoJComboBox.setSelectedItem(Horario.getHoraAtual().getAno());
		 anoJComboBox.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				modificaCalendarioInterno(
						 (Integer) anoJComboBox.getSelectedItem(),
						 (Mes) mesJComboBox.getSelectedItem()
						 );	
			}
		});
		 
		 listaEventosListModel = new DefaultListModel();
		 listaEventosJList = new JList(listaEventosListModel);
		 listaEventosJScrollPane = new JScrollPane(listaEventosJList);
		
		 calendarioInternoJPanel = criaCalendarioInternoJPanel();
		 calendarioInternoJPanel.setBackground(Color.LIGHT_GRAY);
		 modificaCalendarioInterno(
				 (Integer) anoJComboBox.getSelectedItem(),
				 (Mes) mesJComboBox.getSelectedItem()
				 );
		 
		 cadastroEventoJButton = new JButton("Cadastrar Novo");
		 cadastroEventoJButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
            	VisualizadorDeEvento visu = new VisualizadorDeEvento(getJDialog(), true, null, false);
            	visu.anosList.setSelectedItem("" + anoJComboBox.getSelectedItem());
            	visu.diasList.setSelectedItem("" + diaSelecionado);
            	visu.mesesList.setSelectedIndex(mesJComboBox.getSelectedIndex());
            	visu.setVisible(true);	
			}
		});
		 
		 removerEventoJButton = new JButton("Remover");
		 removerEventoJButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				if (listaEventosListModel.getSize() == 0) {
					JDialog aviso = new avisoJDialog("Por favor selecione um item da lista");
					aviso.pack();
					aviso.setVisible(true);
				} else {
					AgendaGrafica.gerente.removeEvento((Evento)listaEventosListModel.getElementAt(listaEventosJList.getSelectedIndex()));
					listaEventosListModel.removeElement(listaEventosListModel.getElementAt(listaEventosJList.getSelectedIndex()));
				}
			} 	
		});
		 
		 voltarJButton = new JButton("Voltar");
		 voltarJButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {	
				getJDialog().dispose();
				AgendaGrafica.atualizaListas.doClick();
				
			}
		});
		 
		 modificaEventoJButton = new JButton("Modificar");
		 modificaEventoJButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				if (listaEventosListModel.getSize() == 0) {
					JDialog aviso = new avisoJDialog("Por favor selecione um item da lista");
					aviso.pack();
					aviso.setVisible(true);
				} else {
					VisualizadorDeEvento dialog = new VisualizadorDeEvento(getJDialog(), true,
	                		(Evento) listaEventosListModel.getElementAt(listaEventosJList.getSelectedIndex()), false);
	                dialog.setVisible(true);
				}
			}
		});
		 
		 
		 constraints.fill = GridBagConstraints.BOTH;
		 constraints.insets = new Insets(5,5,5,5);
		 constraints.weightx = 0.3;
		 constraints.weighty = 0;
		 constraints.gridx = 0;
		 constraints.gridy = 0;
		 constraints.gridheight = 1;
		 constraints.gridwidth = 1;
		 calendarioJPanel.add(mesJComboBox,constraints);
		 
		 constraints.weightx = 0.3;
		 constraints.weighty = 0;
		 constraints.gridx = 1;
		 constraints.gridy = 0;
		 constraints.gridheight = 1;
		 constraints.gridwidth = 1;
		 calendarioJPanel.add(anoJComboBox,constraints);
		 
		 constraints.weightx = 0.1;
		 constraints.weighty = 0.1;
		 constraints.gridx = 0;
		 constraints.gridy = 1;
		 constraints.gridheight = 6;
		 constraints.gridwidth = 2;
		 calendarioJPanel.add(calendarioInternoJPanel,constraints); 
		 
		 constraints.weightx = 0.3;
		 constraints.weighty = 0.5;
		 constraints.gridx = 0;
		 constraints.gridy = 8;
		 constraints.gridheight = 1;
		 constraints.gridwidth = 2;
		 calendarioJPanel.add(listaEventosJScrollPane,constraints); 
		 
		 constraints.weightx = 0.3;
		 constraints.weighty = 0.1;
		 constraints.gridx = 0;
		 constraints.gridy = 9;
		 constraints.gridheight = 1;
		 constraints.gridwidth = 1;
		 calendarioJPanel.add(cadastroEventoJButton,constraints); 
		
		 constraints.weightx = 0.1;
		 constraints.weighty = 0.1;
		 constraints.gridx = 1;
		 constraints.gridy = 9;
		 constraints.gridheight = 1;
		 constraints.gridwidth = 1;
		 calendarioJPanel.add(removerEventoJButton,constraints); 
		 
		 constraints.weightx = 0.1;
		 constraints.weighty = 0.1;
		 constraints.gridx = 0;
		 constraints.gridy = 10;
		 constraints.gridheight = 1;
		 constraints.gridwidth = 1;
		 calendarioJPanel.add(modificaEventoJButton,constraints); 
		 
		 constraints.weightx = 0.1;
		 constraints.weighty = 0.1;
		 constraints.gridx = 1;
		 constraints.gridy = 10;
		 constraints.gridheight = 1;
		 constraints.gridwidth = 1;
		 calendarioJPanel.add(voltarJButton,constraints); 
		 
		 
	 }
	 
	 public int getPrimeiroDiaSemana(int ano, int mes){
		 Calendar calendario = new GregorianCalendar( ano, mes - 1,	 1);  
		 int diaSemana = calendario.get(Calendar.DAY_OF_WEEK);
	     return diaSemana;
	 }
	 
	 public JPanel criaCalendarioInternoJPanel(){
		 JPanel calendarioInternoJPanel = new JPanel(new GridBagLayout()); 
		 GridBagConstraints constraints = new GridBagConstraints();
		 for (int i = 0; i < 6 ; i++){
			 for (int j = 0; j < 7; j++){
				 calendarioJButton[i][j] = new JButton();
				 calendarioJButton[i][j].addActionListener(new ActionListener() {
					
					@Override
					public void actionPerformed(ActionEvent e) {
						
						listaEventosListModel.removeAllElements();
						int dia = 0;
						Mes mes = (Mes) mesJComboBox.getSelectedItem();
						int ano = (Integer) anoJComboBox.getSelectedItem();
						for (int i = 0; i < 6 ; i++){
							for (int j = 0; j < 7; j++){
								if (e.getSource() == calendarioJButton[i][j]){
									dia = Integer.parseInt(calendarioJButton[i][j].getText()); 
									calendarioJButton[i][j].setBackground(Color.CYAN);
								} else {
									calendarioJButton[i][j].setBackground(Color.GRAY);
								}
							}	
						}
						diaSelecionado = dia;
						
						
						ArrayList<Evento> eventos = new ArrayList<Evento>();
						
						for (Evento evento : AgendaGrafica.gerente.getEventos())
							if (evento.iniciaNoDia(dia , mes, ano))
								eventos.add(evento);
						for (Evento evento : eventos)listaEventosListModel.addElement(evento);
							
						
					}
				});
				 constraints.weightx = 0.1;
				 constraints.weighty = 0.1;
				 constraints.insets = new Insets(5,5,5,5);
				 constraints.gridx = j;
				 constraints.gridy = i;
				 constraints.gridheight = 1;
				 constraints.gridwidth = 1;
				 calendarioInternoJPanel.add(calendarioJButton[i][j],constraints);
				 
			 }		 
		}
		return calendarioInternoJPanel;
	 }

	 
			 		 
			
	 public void modificaCalendarioInterno(int ano, Mes mes){
		 int primeiroDiaSemana = getPrimeiroDiaSemana(ano,mes.getNumero());
		 int contador = 1;
		 int dia = 1;
		 diaSelecionado = dia;
		 for (int i = 0; i < 6 ; i++){
			 for (int j = 0; j < 7; j++){
				 if(contador >= primeiroDiaSemana && contador + 1 <= Mes.getNumDiasNoMes(mes, ano) + primeiroDiaSemana){
					 calendarioJButton[i][j].setText("" + dia);
					 calendarioJButton[i][j].setEnabled(true);
					 calendarioJButton[i][j].setBackground(Color.GRAY);
					 if (dia ==  1){
						 calendarioJButton[i][j].setBackground(Color.CYAN);
						 calendarioJButton[i][j].doClick();
					 }
					 dia++;
				 } else {
					 calendarioJButton[i][j].setText("");
					 calendarioJButton[i][j].setEnabled(false);
					 calendarioJButton[i][j].setBackground(Color.GRAY);
				 }
				 contador++;
				
			 }
		 }
	 }

	public JDialog getJDialog(){
		return this;
	}
}
